package com.thecafetechno.main;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.thecafetechno.university.Person;
import com.thecafetechno.university.Student;
import com.thecafetechno.university.Teacher;
import com.thecafetechno.util.HibernateUtil;

public class TPSCTester {
	public static void main(String[] args) {
		Student firstStudent = new Student();
		firstStudent.setName("Saurabh");
		firstStudent.setDepartment("CSE");
		firstStudent.setYear(1);

		Student secondStudent = new Student();
		secondStudent.setName("Pooja");
		secondStudent.setDepartment("CSE");
		secondStudent.setYear(2);

		Student thirdStudent = new Student();
		thirdStudent.setName("Pushpi");
		thirdStudent.setDepartment("CSE");
		thirdStudent.setYear(1);

		Teacher teacher = new Teacher();
		teacher.setName("Dr RN");
		teacher.setDepartment("CSE");
		teacher.setExperience(20);
		teacher.setSpecialization("Grid Computing");

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		session.save(firstStudent);
		// we retrieve this id so that we can load this
		// student later in this program
		Serializable id = session.save(secondStudent);
		session.save(teacher);
		session.save(thirdStudent);
		transaction.commit();

		// retrieving second student
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		Student loadedStudent = (Student) (session.load(Student.class, id));
		transaction.commit();

		System.out.println("\nAfter Load student is: " + loadedStudent);
		
		
		
		List<Person> studentList = session.createQuery("from Person").list();
		 
		for (Person student : studentList) {
			
			System.out.println(student);
			System.out.println(student.getDepartment());
		}

	}

}
